﻿using AutoMapper;
using Dapr.Client;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using todo.Core.Configuration;
using todo.EntityFramework.FromBody;
using todo.EntityFramework.ViewModels.UI;
using todo.user.IServices;

namespace todo.book.API
{
    public  class UsersController : IDynamicApiController
    {
       
        private readonly IDoUserServices _dar;
        private readonly ILogger<UsersController> _logger;
        private readonly DaprClient _daprClient;
        public UsersController(IDoUserServices dar, ILogger<UsersController> logger, DaprClient daprClient)
        {
            _dar = dar;
            _logger = logger;
            _daprClient = daprClient;
        }
        public async Task<AdminUiCallBack> AddBooks(FMBooksInsertModel entity) 
        {
            _logger.LogInformation("[Begin] AddBooks Query User data for");
            // 注意，调用接口返回的是框架的RESTful结构的数据，所以要返回的是应该就是 RESTfulResult<bool>，而不能直接写Task<bool>
            await _daprClient.InvokeMethodAsync<FMBooksInsertModel, RESTfulResult<bool>>
                (HttpMethod.Post, "TodoBooksService", "api/books/is-user-ex-books", entity);
            _logger.LogInformation("[End] AddBooks Query User data for");

            return  await _dar.AddBooks(entity);
        }

        public async Task<AdminUiCallBack> PubSub(FMBooksInsertModel entity)
        {
            _logger.LogInformation("[Begin] PubSub Query User data for");
            // 注意，调用接口返回的是框架的RESTful结构的数据，所以要返回的是应该就是 RESTfulResult<bool>，而不能直接写Task<bool>
            await _daprClient.PublishEventAsync(RedisMessageQueueKey.pubUserExBooks, "neworder", entity);
            _logger.LogInformation("[End] PubSub Query User data for");

            return await _dar.AddBooks(entity);
        }
    }
}
